package org.fire.net;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 协议编码器，将用户自定义消息转换为netty的ByteBuf格式
 * 
 * @author lhl
 *
 */
public class DefaultProtocolEncoder extends MessageToByteEncoder<Packet>
{
	@Override
	protected void encode(ChannelHandlerContext ctx, Packet msg, ByteBuf out) throws Exception
	{
		out.writeShort(msg.getLength());
		if (msg.getBody() != null)
		{
			out.writeBytes(msg.getBody());
		}
	}
}
